/*
 * CupomTroca.java
 *
 * Created on 9 de Janeiro de 2008, 09:57
 */

package br.cefetrn.datinf.pdv.visao;

import br.cefetrn.datinf.estoque.dominio.CupomDeTroca;
import br.cefetrn.datinf.estoque.dominio.ItemVenda;
import br.cefetrn.datinf.estoque.dominio.Venda;
import br.cefetrn.datinf.pdv.ISistema;
import br.cefetrn.datinf.pdv.Sistema;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  wilbert
 */
public class CupomTroca extends javax.swing.JDialog {
    
    private Venda venda;
    private ISistema sis;
    private JFrame parent;
    private DefaultTableModel dtm;
    
    /** Creates new form CupomTroca */
    public CupomTroca(JFrame owner) {
        super(owner);
        this.parent = owner;
        initComponents();
        sis = Sistema.getInstance();
        dtm = (DefaultTableModel) this.jTableProdutos.getModel();
        
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e){
                setVisibleDialog(false);
                dispose();
            }            
        });
       
    }
    
    public void setVisibleDialog( boolean b ){
	parent.setEnabled(!b);
        setVisible(b);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldIdVenda = new javax.swing.JTextField();
        jButtonBuscarVenda = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableProdutos = new javax.swing.JTable();
        jButtonCancelar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        jToolBar1.setRollover(true);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Gerar cupom de troca");

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel1.setText("Gerar cupom de troca");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setText("Id da venda");

        jButtonBuscarVenda.setText("Buscar venda");
        jButtonBuscarVenda.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonBuscarVendaMouseClicked(evt);
            }
        });

        jLabel3.setText("Produtos");

        jTableProdutos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Nome", "Preço", "Quantidade"
            }
        ));
        jScrollPane1.setViewportView(jTableProdutos);

        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonCancelarMouseClicked(evt);
            }
        });

        jButton1.setText("Gerar cupom de troca");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 438, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldIdVenda, javax.swing.GroupLayout.PREFERRED_SIZE, 254, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonBuscarVenda))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(216, 216, 216)
                        .addComponent(jButtonCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldIdVenda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonBuscarVenda))
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButtonCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonBuscarVendaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonBuscarVendaMouseClicked
        // TODO add your handling code here:
        venda = new Venda();        
        
        try{
            venda = sis.buscarVenda(Integer.parseInt(jTextFieldIdVenda.getText()));
            
            for(ItemVenda item : venda.getItens()){
                Object[] it = {item.getId(),item.getNome(), item.getPreco(), item.getQtde()};
                dtm.addRow(it);
            }
         }catch(Exception e){
             JOptionPane.showMessageDialog(null, "Valor inválido");
         }
        
          
    }//GEN-LAST:event_jButtonBuscarVendaMouseClicked

    private void jButtonCancelarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonCancelarMouseClicked
        // TODO add your handling code here:
        setVisibleDialog(false);
    }//GEN-LAST:event_jButtonCancelarMouseClicked

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        // TODO add your handling code here:
        CupomDeTroca cupom = new CupomDeTroca();
        ArrayList<ItemVenda> itensTroca = new ArrayList<ItemVenda>();
        ItemVenda itemVenda;
        double valor = 0;
        
        int[] linhasSelecionadasTabela = jTableProdutos.getSelectedRows();
        System.out.println("Quantidade de linhas selecionadas " +  linhasSelecionadasTabela.length);
        
        for(int i = 0; i< linhasSelecionadasTabela.length; i++){
            itemVenda = new ItemVenda();
            itemVenda.setId(Integer.parseInt(jTableProdutos.getValueAt(i, 0).toString()));
            itemVenda.setNome(jTableProdutos.getValueAt(i, 1).toString());
            itemVenda.setPreco(Double.parseDouble(jTableProdutos.getValueAt(i, 2).toString()));
            itemVenda.setQtde(Integer.parseInt(jTableProdutos.getValueAt(i, 3).toString()));
            
            valor += Double.parseDouble(jTableProdutos.getValueAt(i, 2).toString());
            
            itensTroca.add(itemVenda);
        }
        
        cupom.setItens(itensTroca);
        cupom.setValor(valor);
        cupom.setVenda(venda);
        
        System.out.println(venda.getId());
        
        int numeroCupom = sis.registrarCupomDeTroca(cupom);
        
        cupom.setId(numeroCupom);
        
        imprimirCupomTroca(cupom);
    }//GEN-LAST:event_jButton1MouseClicked
    
    
    private void imprimirCupomTroca(CupomDeTroca cupom){
        CupomTrocaImpress cupomtrocaImpress = new CupomTrocaImpress(cupom);
        cupomtrocaImpress.setVisible(true);
    }
    
    
    /**
     * @param args the command line arguments
     */
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonBuscarVenda;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableProdutos;
    private javax.swing.JTextField jTextFieldIdVenda;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables
    
}
